using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
namespace MicroMsg.Storage
{
	public class StorageIO
	{
		private const string TAG = "StorageIO";
		private const string rootDir = "storage";
		private const string globalConfigDir = "config";
		private const string globalDataDir = "data";
		private const string dataBaseDir = "/db";
		private const string headImageDir = "/head_img";
		private const string headImageQQ = "/head_img_qq";
		private const string headImageTemp = "/head_img_tmp";
		private const string headImgHD = "/head_img_hd";
		public const string thumbImage = "/thumbnail";
		public const string imageDir = "/image";
		public const string voiceDir = "/voice";
		public const string videoDir = "/video";
		private const string accountDir = "/account";
		private const string shakeDir = "/shake";
		private const string massDataDir = "/mass_data";
		private const string configDir = "/config";
		private const string bannerDir = "/banner";
		private const string pluginDir = "/plugin";
		public const string thumbVideo = "/thumbVideo";
		public const string ConversationBgDir = "/Conversation_bg";
		public const string snsDir = "/sns";
		public const string tempDir = "/temp";
		public const string customImageDir = "/custom_img";
		public const string attachmentDir = "/attachment";
		private static string userPath;
		private static string[] userDataDirList = new string[]
		{
			"/db",
			"/head_img",
			"/head_img_tmp",
			"/head_img_hd",
			"/thumbnail",
			"/image",
			"/voice",
			"/video",
			"/account",
			"/shake",
			"/mass_data",
			"/config",
			"/plugin",
			"/banner",
			"/thumbVideo",
			"/Conversation_bg",
			"/sns",
			"/temp",
			"/custom_img",
			"/attachment"
		};
		public static void deleteDir(IsolatedStorageFile isolatedStorageFile_0, string path, bool bDeleteDir = true)
		{
			string[] fileNames = isolatedStorageFile_0.GetFileNames(path + "/*");
			for (int i = 0; i < fileNames.Length; i++)
			{
				string text = fileNames[i];
				isolatedStorageFile_0.DeleteFile(path + "/" + text);
				Log.smethod_5("storage", "delete file " + path + "/" + text);
			}
			string[] directoryNames = isolatedStorageFile_0.GetDirectoryNames(path + "/*");
			for (int j = 0; j < directoryNames.Length; j++)
			{
				string text2 = directoryNames[j];
				StorageIO.deleteDir(isolatedStorageFile_0, path + "/" + text2, bDeleteDir);
			}
			if (bDeleteDir)
			{
				isolatedStorageFile_0.DeleteDirectory(path);
				Log.smethod_5("storage", "delete dir " + path);
			}
		}
		public static void deleteAllUserData()
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					StorageIO.deleteDir(userStoreForApplication, "storage", true);
				}
			}
			catch
			{
				Log.smethod_5("storage", "delete file fail");
			}
		}
		public static void deleteMsgData()
		{
			string[] array = new string[]
			{
				StorageIO.getThumbnailPath(),
				StorageIO.getImagePath(),
				StorageIO.getVoicePath(),
				StorageIO.getVideoPath()
			};
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					string[] array2 = array;
					for (int i = 0; i < array2.Length; i++)
					{
						string text = array2[i];
						string[] fileNames = userStoreForApplication.GetFileNames(text + "/*");
						for (int j = 0; j < fileNames.Length; j++)
						{
							string text2 = fileNames[j];
							if (userStoreForApplication.FileExists(text + "/" + text2))
							{
								userStoreForApplication.DeleteFile(text + "/" + text2);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "deleteMsgData  exception  " + ex);
			}
		}
		public static bool writeToFile(string fileName, byte[] data, bool bCreateDir = false)
		{
			if (!string.IsNullOrEmpty(fileName) && data != null)
			{
				try
				{
					using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
					{
						if (bCreateDir)
						{
							string directoryName = Path.GetDirectoryName(fileName);
							if (!userStoreForApplication.DirectoryExists(directoryName))
							{
								userStoreForApplication.CreateDirectory(directoryName);
							}
						}
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(fileName, 2, userStoreForApplication))
						{
							isolatedStorageFileStream.Write(data, 0, data.Length);
						}
					}
					return true;
				}
				catch (Exception ex)
				{
					Log.smethod_5("storage", "writeToFile file fail" + ex);
				}
				return false;
			}
			return false;
		}
		public static void writeToFile(string fileName, int offset, params byte[][] dataList)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(fileName, 4, userStoreForApplication))
					{
						isolatedStorageFileStream.Seek((long)offset, 0);
						for (int i = 0; i < dataList.Length; i++)
						{
							byte[] array = dataList[i];
							isolatedStorageFileStream.Write(array, 0, array.Length);
						}
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "writeToFile file fail" + ex);
			}
		}
		public static void appendToFile(string fileName, byte[] data)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(fileName, 4, userStoreForApplication))
					{
						isolatedStorageFileStream.Seek(0L, 2);
						isolatedStorageFileStream.Write(data, 0, data.Length);
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "writeToFile file fail" + ex);
			}
		}
		public static byte[] readFromFile(string fileName)
		{
			if (string.IsNullOrEmpty(fileName))
			{
				return null;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					byte[] result;
					if (userStoreForApplication.FileExists(fileName))
					{
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(fileName, 3, userStoreForApplication))
						{
							if (isolatedStorageFileStream.get_Length() <= 0L)
							{
								result = null;
								return result;
							}
							byte[] array = new byte[isolatedStorageFileStream.get_Length()];
							int num = isolatedStorageFileStream.Read(array, 0, array.Length);
							if ((long)num != isolatedStorageFileStream.get_Length())
							{
								Log.smethod_1("StorageIO", string.Concat(new object[]
								{
									"readFromFile error, read count = ",
									isolatedStorageFileStream.get_Length(),
									" , ret = ",
									num
								}));
								result = null;
								return result;
							}
							result = array;
							return result;
						}
					}
					result = null;
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", string.Concat(new object[]
				{
					"readFromFile file fail ",
					fileName,
					" ",
					ex
				}));
			}
			return null;
		}
		public static byte[] readFromFile(string fileName, int offset, int count)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(fileName, 3, userStoreForApplication))
					{
						isolatedStorageFileStream.Seek((long)offset, 0);
						byte[] array = new byte[count];
						int num = isolatedStorageFileStream.Read(array, 0, array.Length);
						byte[] result;
						if (num != count)
						{
							Log.smethod_1("StorageIO", string.Concat(new object[]
							{
								"readFromFile error, read count = ",
								count,
								" , ret = ",
								num
							}));
							result = null;
							return result;
						}
						result = array;
						return result;
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_5("storage", "readFromFile file fail" + ex);
			}
			return null;
		}
		public static long getFileExistTime(string path)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					long result;
					if (!userStoreForApplication.FileExists(path))
					{
						result = 0L;
						return result;
					}
					result = (long)DateTime.get_Now().Subtract(userStoreForApplication.GetCreationTime(path).get_DateTime()).get_TotalSeconds();
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "getCreationTime file fail" + ex);
			}
			return 0L;
		}
		public static long fileLength(string path)
		{
			if (string.IsNullOrEmpty(path))
			{
				return 0L;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					long result;
					if (userStoreForApplication.FileExists(path))
					{
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(path, 3, userStoreForApplication))
						{
							result = isolatedStorageFileStream.get_Length();
							return result;
						}
					}
					result = 0L;
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "fileLength Exception " + ex);
			}
			return 0L;
		}
		public static bool fileExists(string path)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					return userStoreForApplication.FileExists(path);
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "fileExists Exception " + ex);
			}
			return false;
		}
		public static bool dirExists(string path)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					return userStoreForApplication.DirectoryExists(path);
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "fileExists Exception " + ex);
			}
			return false;
		}
		public static bool deleteFile(string fileName)
		{
			if (string.IsNullOrEmpty(fileName))
			{
				return false;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (userStoreForApplication.FileExists(fileName))
					{
						userStoreForApplication.DeleteFile(fileName);
					}
				}
				return true;
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", string.Concat(new object[]
				{
					"DeleteFile file fail ",
					fileName,
					" \n",
					ex
				}));
			}
			return false;
		}
		public static bool emptyDir(string strPath)
		{
			if (string.IsNullOrEmpty(strPath))
			{
				return false;
			}
			try
			{
				bool result;
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(strPath))
					{
						result = false;
						return result;
					}
					string[] fileNames = userStoreForApplication.GetFileNames(strPath + "/*");
					for (int i = 0; i < fileNames.Length; i++)
					{
						string text = fileNames[i];
						userStoreForApplication.DeleteFile(strPath + "/" + text);
						Log.smethod_5("StorageIO", "delete file " + strPath + "/" + text);
					}
					string[] directoryNames = userStoreForApplication.GetDirectoryNames(strPath + "/*");
					for (int j = 0; j < directoryNames.Length; j++)
					{
						string text2 = directoryNames[j];
						StorageIO.deleteDir(userStoreForApplication, strPath + "/" + text2, true);
						Log.smethod_5("StorageIO", "delete dir " + strPath + "/" + text2);
					}
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				Log.smethod_1("StorageIO", string.Concat(new object[]
				{
					"emptyDir clear path fail=",
					strPath,
					" \n",
					ex
				}));
			}
			return false;
		}
		public static bool createDir(string strPath)
		{
			if (string.IsNullOrEmpty(strPath))
			{
				return false;
			}
			try
			{
				bool result;
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (userStoreForApplication.DirectoryExists(strPath))
					{
						result = true;
						return result;
					}
					userStoreForApplication.CreateDirectory(strPath);
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				Log.smethod_1("StorageIO", "createDir  path fail=" + ex);
			}
			return false;
		}
		public static bool emptyFile(string strPath)
		{
			if (string.IsNullOrEmpty(strPath))
			{
				return false;
			}
			try
			{
				bool result;
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(strPath))
					{
						result = false;
						return result;
					}
					StorageIO.deleteDir(userStoreForApplication, strPath, false);
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				Log.smethod_1("StorageIO", string.Concat(new object[]
				{
					"emptyFile clear path fail=",
					strPath,
					" \n",
					ex
				}));
			}
			return false;
		}
		public static int filesNumber(string strPath)
		{
			if (string.IsNullOrEmpty(strPath))
			{
				return 0;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					int result;
					if (!userStoreForApplication.DirectoryExists(strPath))
					{
						result = 0;
						return result;
					}
					result = StorageIO.countFilesNumber(userStoreForApplication, strPath);
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("StorageIO", string.Concat(new object[]
				{
					"countFiles Exception=",
					strPath,
					" \n",
					ex
				}));
			}
			return 0;
		}
		private static int countFilesNumber(IsolatedStorageFile isolatedStorageFile_0, string strPath)
		{
			int num = isolatedStorageFile_0.GetFileNames(strPath + "/*").Length;
			string[] directoryNames = isolatedStorageFile_0.GetDirectoryNames(strPath + "/*");
			for (int i = 0; i < directoryNames.Length; i++)
			{
				string text = directoryNames[i];
				num += StorageIO.countFilesNumber(isolatedStorageFile_0, strPath + "/" + text);
			}
			return num;
		}
		public static List<string> dirsDetail(string strPath)
		{
			if (string.IsNullOrEmpty(strPath))
			{
				return null;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					List<string> result;
					if (!userStoreForApplication.DirectoryExists(strPath))
					{
						result = null;
						return result;
					}
					List<string> list = new List<string>();
					string[] directoryNames = userStoreForApplication.GetDirectoryNames(strPath + "/*");
					for (int i = 0; i < directoryNames.Length; i++)
					{
						string text = directoryNames[i];
						list.Add(strPath + "/" + text);
					}
					result = list;
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("StorageIO", string.Concat(new object[]
				{
					"dirsDetail Exception=",
					strPath,
					" \n",
					ex
				}));
			}
			return null;
		}
		public static List<FileDetail> filesDetail(string strPath)
		{
			if (string.IsNullOrEmpty(strPath))
			{
				return null;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					List<FileDetail> result;
					if (!userStoreForApplication.DirectoryExists(strPath))
					{
						result = null;
						return result;
					}
					result = StorageIO.getFilesDetail(strPath, userStoreForApplication, new List<FileDetail>());
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("StorageIO", string.Concat(new object[]
				{
					"getFileDetail Exception=",
					strPath,
					" \n",
					ex
				}));
			}
			return null;
		}
		private static List<FileDetail> getFilesDetail(string strPath, IsolatedStorageFile isolatedStorageFile_0, List<FileDetail> fileList)
		{
			string[] fileNames = isolatedStorageFile_0.GetFileNames(strPath + "/*");
			for (int i = 0; i < fileNames.Length; i++)
			{
				string text = fileNames[i];
				FileDetail fileDetail = new FileDetail();
				fileDetail.strFileName = strPath + "/" + text;
				fileDetail.idleMinutes = (long)Math.Abs((int)(DateTime.get_Now() - isolatedStorageFile_0.GetLastAccessTime(fileDetail.strFileName)).get_TotalMinutes());
				fileList.Add(fileDetail);
			}
			string[] directoryNames = isolatedStorageFile_0.GetDirectoryNames(strPath + "/*");
			for (int j = 0; j < directoryNames.Length; j++)
			{
				string text2 = directoryNames[j];
				StorageIO.getFilesDetail(strPath + "/" + text2, isolatedStorageFile_0, fileList);
			}
			return fileList;
		}
		public static void initRootDir()
		{
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				if (!userStoreForApplication.DirectoryExists("storage"))
				{
					userStoreForApplication.CreateDirectory("storage");
				}
				if (!userStoreForApplication.DirectoryExists("config"))
				{
					userStoreForApplication.CreateDirectory("config");
				}
				if (!userStoreForApplication.DirectoryExists("data"))
				{
					userStoreForApplication.CreateDirectory("data");
				}
			}
		}
		public static void initGlobalConfigRootDir()
		{
			StorageIO.initRootDir();
		}
		private static string getXmlFileName()
		{
			return StorageIO.userPath + "/config/io.xml";
		}
		private static bool needInitUserDir()
		{
			StorageIOInfo storageIOInfo = StorageXml.loadObject<StorageIOInfo>(StorageIO.getXmlFileName());
			return storageIOInfo == null || storageIOInfo.lastInitClientVersion != ConstantsProtocol.CLIENT_VERSION;
		}
		private static void setInitUserReady()
		{
			StorageIOInfo storageIOInfo = StorageXml.loadObject<StorageIOInfo>(StorageIO.getXmlFileName());
			if (storageIOInfo != null && storageIOInfo.lastInitClientVersion == ConstantsProtocol.CLIENT_VERSION)
			{
				return;
			}
			if (storageIOInfo == null)
			{
				storageIOInfo = new StorageIOInfo();
			}
			storageIOInfo.lastInitClientVersion = ConstantsProtocol.CLIENT_VERSION;
			StorageXml.saveObject<StorageIOInfo>(storageIOInfo, StorageIO.getXmlFileName());
		}
		public static void initUserDataDir(string userName)
		{
			StorageIO.userPath = "storage/" + userName;
			if (!StorageIO.needInitUserDir())
			{
				return;
			}
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				userStoreForApplication.CreateDirectory(StorageIO.userPath);
				string[] array = StorageIO.userDataDirList;
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i];
					userStoreForApplication.CreateDirectory(StorageIO.userPath + text);
				}
			}
			StorageIO.setInitUserReady();
		}
		public static string getRooDir()
		{
			return "storage";
		}
		public static string getUserPath()
		{
			return StorageIO.userPath;
		}
		public static string getDatabasePath()
		{
			return StorageIO.userPath + "/db";
		}
		public static string getHeadImgPath()
		{
			return StorageIO.userPath + "/head_img";
		}
		public static string getHeadImgQQPath()
		{
			return StorageIO.userPath + "/head_img_qq";
		}
		public static string getHeadImgTempPath()
		{
			return StorageIO.userPath + "/head_img_tmp";
		}
		public static string getHeadImgHDPath()
		{
			return StorageIO.userPath + "/head_img_hd";
		}
		public static string getThumbnailPath()
		{
			return StorageIO.userPath + "/thumbnail";
		}
		public static string getImagePath()
		{
			return StorageIO.userPath + "/image";
		}
		public static string getVoicePath()
		{
			return StorageIO.userPath + "/voice";
		}
		public static string getVideoPath()
		{
			return StorageIO.userPath + "/video";
		}
		public static string getAccountPath(string userName)
		{
			return "storage/" + userName + "/account";
		}
		public static string getShakePath()
		{
			return StorageIO.userPath + "/shake";
		}
		public static string getMassDataPath()
		{
			return StorageIO.userPath + "/mass_data";
		}
		public static string getConfigPath()
		{
			return StorageIO.userPath + "/config";
		}
		public static string getGlobalConfigPath()
		{
			return "config";
		}
		public static string getGlobaDataPath()
		{
			return "data";
		}
		public static string getPluginRootPath()
		{
			return StorageIO.userPath + "/plugin";
		}
		public static string getPluginPath(string pluginName)
		{
			return StorageIO.userPath + "/plugin/" + pluginName;
		}
		public static string getBannerPath()
		{
			return StorageIO.userPath + "/banner";
		}
		public static string getConversationBgPath()
		{
			return StorageIO.userPath + "/Conversation_bg";
		}
		public static string getUserPath(string string_0)
		{
			return StorageIO.userPath + string_0;
		}
		public static string getSnsPath()
		{
			return StorageIO.userPath + "/sns";
		}
		public static string getTempPath()
		{
			return StorageIO.userPath + "/temp";
		}
		public static string getCustomImagePath()
		{
			return StorageIO.userPath + "/custom_img";
		}
		public static string getAttachmentPath()
		{
			return StorageIO.userPath + "/attachment";
		}
		public static void testDeletFile()
		{
			DebugEx.getTimeSpan();
			IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
			for (int i = 0; i < 300; i++)
			{
				userStoreForApplication.DeleteFile("test/" + i);
			}
			userStoreForApplication.Dispose();
		}
		public static void testCreateFile()
		{
			DebugEx.getTimeSpan();
			byte[] array = new byte[10];
			IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
			userStoreForApplication.CreateDirectory("test");
			for (int i = 0; i < 300; i++)
			{
				using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("test/" + i, 2, userStoreForApplication))
				{
					isolatedStorageFileStream.Write(array, 0, 10);
				}
			}
			for (int i = 0; i < 300; i++)
			{
				userStoreForApplication.GetLastAccessTime("test/" + i);
			}
			userStoreForApplication.Dispose();
		}
		public static void test()
		{
		}
	}
}
